home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9272 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: kuhub.cc.ukans.edu!anh
  2. From: anh@kuhub.cc.ukans.edu
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Tradition or what?
  5. Message-ID: <1996Mar8.153250.115645@kuhub.cc.ukans.edu>
  6. Date: 8 Mar 96 15:32:49 CST
  7. References: <4g0elg$mdr@redstone.interpath.net> <4hpd8a$d70@alterdial.UU.NET>
  8. Organization: University of Kansas Academic Computing Services
  9.  
  10. Well, I found one good use of magic numbers such as when one needs a 
  11. localized temporary buffer of data.
  12.  
  13. int func()
  14. {
  15.     FILE *fp;
  16.     char buf[15+1];
  17.     
  18.     ...
  19.  
  20.     fgets(buf,15,fp);
  21. }
  22.  
  23.  
  24. Well, if I know the data is always going to be less than 15 or whatever, 
  25. there is really no need to use a #define here.
  26.  
  27. CAnh
  28.  
  29. In article <4hpd8a$d70@alterdial.UU.NET>, rogerst@approach.com (Tom Rogers) writes:
  30. > softbase@mercury.interpath.net (Scott McMahan - Softbase Systems)
  31. > wrote:
  32. >>Larry Weiss (lfw@oc.com) wrote:
  33. >>  
  34. >>: > Actually, the second rule is quite good (except that '2' should be replaced
  35. >>: > by '1' :-)  Magic constants embedded in the code are a huge nuisance
  36. >>: > for maintenability.  [...]
  37. >>: I agree that there are good uses for giving a literal a name, and using
  38. >>: that name instead of repeated usage of the literal, but to make a
  39. >>: mandate that even if the literal were referenced only once you must give
  40. >>: it a name would seem too extreme.
  41. >>From personal experience, I think you can't be too extreme. I've been
  42. >>burned by literals before. I've come to the conclusion that magic
  43. >>numbers in the code are evil and should be done away with. Anything
  44. >>that isn't a universal, unchanging constant (like an integer being 32
  45. >>bits :)), something like a law of physics, needs to be a constant in
  46. >>the program. I always #define it, too, since C has broken const
  47. >>constants that can't be used as initializers (which defeats the whole
  48. >>point of const).
  49. >>Scott
  50. > I agree, Scott.   Magic numbers are evil.  Don't use them not even
  51. > once.  Your code will be reused, by you or someone else.  Even if
  52. > you just use a magic number in one place it doesn't mean that in
  53. > the future your routines won't get included in some other module
  54. > with other new routines that need to use the 'same' magic number.
  55. > Furthermore, what does 5 or 7 or 10 mean?  It is NOT self documenting.
  56. > Make it a well named constant and reduce the need for accompanying
  57. > comments describing what the value is.
  58. > Tom
  59.